package com.woniuxy.microgoodsserver.attributemag.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.AttributeInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo2;
import com.woniuxy.microcommonserver.bean.gy.ValueInfo;
import com.woniuxy.microgoodsserver.attributemag.mapper.AttributeMapper;
import com.woniuxy.microgoodsserver.attributemag.service.AttributeService;
import com.woniuxy.microgoodsserver.sortmag.service.GoodCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**规格进行增删查改
 * @author ：苟杨
 * @description：TODO
 * @date ：2021/1/23 14:18
 */
@Service
@Transactional(readOnly=true)
public class AttributeServiceImpl implements AttributeService {

    @Resource
    private AttributeMapper attributeMapper;
    @Resource
    private GoodCategoryService goodCategoryServiceImpl;
    /**
        新增规格
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void addAttribute(AttributeInfo attributeInfo, List<String> valueInfoList) {

        //根据规格所属分类查询该子类的ID
        GoodCategoryInfo2 categoryInfo2 = goodCategoryServiceImpl.findSonIdByName(attributeInfo.getRemark());

        //添加规格
        attributeMapper.addAttribute(attributeInfo,categoryInfo2.getId());

        //查询添加的规格值的ID，为规格添加属性值(根据备注和规格名称查询唯一ID)
        Integer attributeId = attributeMapper.findAttributeId(attributeInfo);
//        循环添加属性值
        List<String> l=new ArrayList();
        valueInfoList.forEach(e->{
            //格式转换
            List<String> s = Arrays.asList(e.replaceAll("[{]", "").replaceAll("[}]", "").split("="));
            l.add(s.get(1));
            //[value, 红色]
            //[sort, 1]
            //[value, 黑色]
            //[sort, 2]得到这样的数组
        });
        System.out.println(l);
        ValueInfo vi = new ValueInfo();
        //将得到的List数组进行判断赋值
        for(int i=0;i<l.size();i++){
            if(i%2==0){
                vi.setValue(l.get(i));
                vi.setSort(Integer.parseInt(l.get(i+1)));
                attributeMapper.addValues(vi,attributeId);
            }
        }
    }
    /**
        查询所有的商品
     * @params:
     * @return
     */
    @Override
    public PageInfo<AttributeInfo> findAllAttribute(int pageNum, int pageSize) {
        return PageHelper.startPage(pageNum,pageSize).
                doSelectPageInfo(()->attributeMapper.findAllAttribute());
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void deleteAttribute(AttributeInfo attributeInfo) {
        attributeMapper.deleteAttribute(attributeInfo);
    }

    @Override
    public List<Map> findAttributeById(AttributeInfo attributeInfo) {


        return attributeMapper.findAttributeById(attributeInfo);
    }

    /**
        删除规格值
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public void deleteValue(Integer vid) {
        attributeMapper.deleteValue(vid);
    }

    /**
        修改规格和其属性值
     * @params:
     * @return
     */
    @Override
    @Transactional(rollbackFor=Exception.class,propagation = Propagation.REQUIRED)
    public void updateAttribute(AttributeInfo attributeInfo, List<String> valueInfos) {
        //如果要做跨子目修改，先根据备注名字查询子目的ID
        GoodCategoryInfo2 goodCategoryInfo2 = goodCategoryServiceImpl.findSonIdByName(attributeInfo.getRemark());
        //得到子目的ID
        Integer goodCategoryInfo2Id=goodCategoryInfo2.getId();
        System.out.println(goodCategoryInfo2Id);
        //修改数据
        attributeMapper.updateAttribute(attributeInfo,goodCategoryInfo2Id);
        //判断规格值ID是否有新增，根据规格传过来的规格值ID去数据库查询是否存在
        //循环添加属性值
        List<String> l=new ArrayList();
        valueInfos.forEach(e->{
            //格式转换
            //[1,value, 红色]
            //[2,sort, 1]
            //[3,value, 黑色]
            //[4,sort, 2]得到这样的数组
            List<String> s = Arrays.asList(e.replaceAll("[{]", "").replaceAll("[}]", "").split("="));
            System.out.println(s);
            l.add(s.get(1));

        });
        System.out.println(l);
        ValueInfo vi = new ValueInfo();
        //将得到的List数组进行判断赋值
        for(int i=0;i<l.size();i++){
            if(i%3==1){
                //根据value去查询是否存在
                ValueInfo valueById = attributeMapper.findValueById(l.get(i));
                //判断是否为空，如果为空说明需要新增规格值
                if(valueById==null){
                    vi.setValue(l.get(i));
                    vi.setSort(Integer.parseInt(l.get(i+1)));
                    attributeMapper.addValues(vi,attributeInfo.getId());
                }
            }
        }

    }


    /**
     根据分类ID查询规格名称和其规格属性值
     * @params:
     * @return
     */
    @Override
    public List<Map> findAttributeAndValue(Integer sortId) {
        return attributeMapper.findAttributeAndValue(sortId);
    }
}
